<?php


namespace App\Job;


use App\Config\QueueConfig;
use App\Enum\LockHistoryEnum;
use App\Http\Library\Paas;
use App\Http\Library\Queue;
use App\Http\Model\Job\DwModel;

class DwJob
{

    /*本次任务涉及到的监管账户*/
    private static $pfs = [];


    /**
     * 读取数仓中间库数据-获取要同步招行的数据
     *
     * @param array $data
     *
     * @return array
     * @throws \Exception
     */
    public static function main(array $data)
    {
        try {
            /*任务开始*/
            if (empty($data['offset_id'])) {
                /*重置本次任务涉及到的监管账户*/
                self::$pfs = [];
                /*更新任务进度*/
                Queue::rpush([
                    'data_date' => $data['data_date'],
                    'db'        => $data['db'],
                    'table'     => $data['table'],
                    'status'    => LockHistoryEnum::STATUS_ING,
                ], QueueConfig::LOCK_HISTORY);
            }
            /*分页偏移ID*/
            $offsetId = empty($data['offset_id']) ? 0 : $data['offset_id'];
            /*主体ID*/
            $ledgerSubjectId = null;
            /*汇总日期*/
            $createDate = null;
            $merge = [];
            /*数仓-发生收退费行为的班级列表*/
            if ($data['table'] === LockHistoryEnum::DWD_FIN_CAPITAL_SUPERVISION_CLASS_LIST_ADD_FD) {
                /*获取需要写入任务队列的数据*/
                $list = DwModel::listClass($data, $offsetId, $ledgerSubjectId);
                /*招行-班级信息同步*/
                $task = QueueConfig::PFS_HEAD_ADDBATCHCLASS;
            }
            /*数仓-当日推送缴退费汇总表PayClassList（按班级汇总）*/
            if ($data['table'] === LockHistoryEnum::DWS_FIN_CAPITAL_SUPERVISION_PAY_CLASS_LIST_RESULT_FD) {
                /*获取需要写入任务队列的数据*/
                $list = DwModel::addClassIncrement($data, $offsetId, $ledgerSubjectId, $createDate);
                /*计算当天要推送的数据总条数*/
                $merge['add_class_increment_total'] = DwModel::addClassIncrementTotal($data, $ledgerSubjectId);
                /*招行-售/退课汇总信息同步*/
                $task = QueueConfig::PFS_HEAD_ADDCLASSINCREMENT;
            }
            /*数仓-班级划拨计划表TransferList*/
            if ($data['table'] === LockHistoryEnum::DWD_FIN_CAPITAL_SUPERVISION_TRANSFER_LIST_FD) {
                /*获取需要写入任务队列的数据*/
                $list = DwModel::pfsHeadAddbatchtransfer($data, $offsetId, $ledgerSubjectId);
                /*3.2招行-提出划拨/解冻   机构端 -> 招行*/
                $task = QueueConfig::PFS_HEAD_ADDBATCHTRANSFER;
            }
            /*数仓-资金监管排课信息*/
            if ($data['table'] === LockHistoryEnum::DWS_FIN_CAPITAL_SUPERVISION_LESSON_LIST_AKS_FD) {
                $list = DwModel::pfsHeadAddClassLesson($data, $offsetId, $ledgerSubjectId);
                /*2.4 排课信息同步   机构端 -> 招行*/
                $task = QueueConfig::PFS_HEAD_ADDCLASSLESSON;
            }
            /*数据分页读取完毕,则直接返回*/
            if (empty($list)) {
                /*空白表预警*/
                if ($offsetId === 0) {
                    log_arr([
                        '[数仓取数空白]',
                        'data' => serialize($data),
                    ], 'pfs');
                    Paas::yach([
                        '[数仓取数空白]',
                        'data' => serialize($data),
                    ]);
                }
                /*更新任务进度*/
                Queue::rpush([
                    'data_date' => $data['data_date'],
                    'db'        => $data['db'],
                    'table'     => $data['table'],
                    'status'    => LockHistoryEnum::STATUS_SUCCESS,
                ], QueueConfig::LOCK_HISTORY);
                /*数仓-当日推送缴退费汇总表PayClassList（按班级汇总）*/
                if ($data['table'] === LockHistoryEnum::DWS_FIN_CAPITAL_SUPERVISION_PAY_CLASS_LIST_RESULT_FD) {
                    if (!empty(self::$pfs)) {
                        foreach (self::$pfs as $val) {
                            /*汇总数据同时完成后,执行当日班级&账户轧差净额计算*/
                            Queue::rpush([
                                'pfs_dw_id'   => $val['pfs_id'],
                                'sleep'       => 5,
                                'create_date' => $val['create_date']
                            ], QueueConfig::CLASS_NETTING);
                        }
                        self::$pfs = [];
                    }
                    /*3.10>班级拨付余额查询>机构端->银行*/
                    Queue::rpush([
                        'sleep' => 5
                    ], QueueConfig::CLASS_AMT_START);
                }
                return [];
            }
            /*更新分页偏移量*/
            $data['offset_id'] = $offsetId;
            /*查询本次处理数据的监管主体信息*/
            $pfs = DwModel::pfs($ledgerSubjectId);
            if (empty($pfs)) {
                log_arr(['[job:dw]', 'msg:监管账户不存在', 'list' => serialize($list), 'pfs' => $pfs], 'pfs');
                throw new \Exception('监管账户不存在');
            }
            /*如果是汇总任务,则记录*/
            if ($data['table'] === LockHistoryEnum::DWS_FIN_CAPITAL_SUPERVISION_PAY_CLASS_LIST_RESULT_FD) {
                self::$pfs["{$pfs->id}.{$createDate}"] = ['pfs_id' => $pfs->id, 'create_date' => $createDate];
            }
            Queue::lpush($data);
            /*要同步的数据写入队列*/
            Queue::rpush(array_merge([
                'list'                => $list,
                /*招行&数仓-监管账户主体配置信息 表主键ID*/
                'pfs_dw_id'           => $pfs->id,
                /*招行&数仓-监管账户主体配置信息 机构编码*/
                'pfs_agencyid'        => $pfs->pfs_agencyid,
                /*招行-加密密钥*/
                'pfs_encrypt_private' => $pfs->pfs_encrypt_private,
                /*招行-签名密钥*/
                'pfs_sign_private'    => $pfs->pfs_sign_private,
                /*主体*/
                'ledger_subject_id'   => $pfs->ledger_subject_id,
                'ledger_subject_name' => $pfs->ledger_subject_name,
                /*城市*/
                'city_name'           => $pfs->city_name,
                'area_code'           => $pfs->area_code,
            ], $merge), $task);
        } catch (\Exception $exception) {
            $err = [
                '[job:dw]-异常',
                'line' => $exception->getLine(),
                'file' => $exception->getFile(),
                'msg'  => $exception->getMessage(),
                'data' => serialize($data)
            ];
            log_arr($err, 'pfs');
            if (config('app.debug') === true) {
                var_dump($err);
            } else {
                /*调用预警推送通知*/
                Paas::yach($err);
            }
            throw new \Exception($exception);
        }
    }

}





/**
 * DwJob.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/4 21:21:00   操作:创建
 **/
